package com.kkb.mybatis.config;

import com.kkb.mybatis.utils.DocumentUtils;
import org.dom4j.Document;
import org.dom4j.Element;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;


/**
 * User: zsl
 * Date: 2019/10/1011:24
 * Description: 解析全局配置文件 sqlMapConfig.xml 文件
 */
public class XMLConfigParser {

    private  Configuration configuration;
    //为什么这么写,等于在new对象的时候直接new了一个 configuration 对象
    public XMLConfigParser() {
        configuration = new Configuration();
    }

    /**
     * 开始解析 sqlMapCOnfig.xml 的根标签 <configuration> 和它里边的标签
     *
     * @param rootElement
     * @return
     */
    public Configuration parse(Element rootElement) {
        //解析 <environments> 标签
        parseEnvironments(rootElement.element("environments"));
//        //解析 mappers 标签
        parseMappers(rootElement.element("mappers"));
        return configuration;
    }



    /**
     * 解析 sqlMapConfigure <environments> 标签
     * @param element
     */
    private void parseEnvironments(Element element) {
        String aDefault = element.attributeValue("default");
        if(aDefault == "" || "".equals(aDefault)){
            return;
        }
        List<Element> environment = element.elements("environment");
        for (Element envElement : environment) {
            String typeId = envElement.attributeValue("id");
            //判断 id 是否和<environments> 中default 中的值一致,如果一致才去解析
            if(aDefault.equals(typeId)){
                parseEnvironment(envElement);
            }

        }
    }

    /**
     * 解析 <environment> 中的 dataSource 中的标签
     * @param element
     */
    private void parseEnvironment(Element element) {
        Element dataSourceEnv = element.element("dataSource");
        String type = dataSourceEnv.attributeValue("type");
        type = type == null || "".equals(type) ?"DBCP" : type;
        if("DBCP".equals(type)){
            Properties properties = parseProperty(dataSourceEnv);

            BaseDataSource baseDataSource = new BaseDataSource();
            baseDataSource.setDriver(properties.getProperty("driver"));
            baseDataSource.setPassword(properties.getProperty("password"));
            baseDataSource.setUrl(properties.getProperty("url"));
            baseDataSource.setUsername(properties.getProperty("username"));

            configuration.setBaseDataSource(baseDataSource);

        }
    }

    /**
     *  获取sqlmapConfig.xml 中的 property 标签
     * @param dataSourceEnv
     * @return
     */
    private Properties parseProperty(Element dataSourceEnv) {
        Properties properties = new Properties();
        List<Element> property = dataSourceEnv.elements("property");
        for (Element element : property) {
            properties.put(element.attributeValue("name"),element.attributeValue("value"));
        }
        return properties;
    }

    /**
     * 解析 sqlMapConfigure <mapper> 标签
     * @param element
     */
    private void parseMappers(Element element) {
        List<Element> mapper = element.elements("mapper");
        for (Element element1 : mapper) {
            parseMapper(element1);
        }
    }

    private void parseMapper(Element element1) {
        //获取映射文件的路径
        String resource = element1.attributeValue("resource");
        //获取mapper.xml的数据
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        Document document = DocumentUtils.readDocument(resourceAsStream);
        //准备解析 mapper.xml
        XMLMapperParser mapperParser = new XMLMapperParser(configuration);

        mapperParser.parse(document.getRootElement());
    }

}
